package com.bupt.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.bupt.dto.Result;
import com.bupt.pojo.ShopType;
import com.bupt.mapper.ShopTypeMapper;
import com.bupt.mapper.UserMapper;
import com.bupt.service.IShopTypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 虎哥
 * @since 2021-12-22
 */
@Service
public class ShopTypeServiceImpl extends ServiceImpl<ShopTypeMapper, ShopType> implements IShopTypeService {

    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @Resource
    private UserMapper userMapper;


    @Override
    public Result queryTypeLists() {
        String s = userMapper.countUserTotal();
        System.out.println(s);
        String shopTypeJSON = stringRedisTemplate.opsForValue().get("shopType: shopTypes" );
        if (StrUtil.isNotBlank(shopTypeJSON)) {
            List<ShopType> shopType = JSONUtil.toList(shopTypeJSON, ShopType.class);//tolist用法类似tobean
            return Result.ok(shopType);
        }
        List<ShopType> shopTypes = query().orderByAsc("sort").list();
        String listJSON = JSONUtil.toJsonStr(shopTypes);
        if (shopTypes == null){
            return Result.fail("查询失败");
        }
        stringRedisTemplate.opsForValue().set("shopType: shopTypes",listJSON);
        return Result.ok(shopTypes);
    }
}
